#ifndef __SIMPLECHATGUI_CHATCLIENT_H__
#define __SIMPLECHATGUI_CHATCLIENT_H__

#include "netserver.h"

#include <QObject>
#include <boost/optional.hpp>
#include <thread>

class QtNetClient : public QObject
{
    Q_OBJECT
public:
    typedef boost::asio::ip::tcp::socket TcpSocket;
    typedef boost::asio::ip::tcp::resolver::iterator endPointIteratorType;

    QtNetClient(endPointIteratorType endpoint_iterator);

    void write(const NetChatMsg& a_msg);
    void close();

private:

    void handle_connect(const boost::system::error_code &);
    void handle_read_header(const boost::system::error_code &);
    void handle_read_body(const boost::system::error_code &);
    void do_write(NetChatMsg);
    void handle_write(const boost::system::error_code &);
    void do_close();

signals:
    void receivedMsg(const QString &);

private:
    TcpSocket          m_socket;
    NetChatMsg         m_read_msg;
    chat_message_queue m_write_msgs;
};

class QtChatClient : public QObject
{
    Q_OBJECT
public:

    QtChatClient();
    virtual ~QtChatClient();

public slots:

    void setPseudonyme(const QString &a_pseudo);
    void sendStringMsg(const QString &a_msg);
    void connectServer(const QString &a_host,const QString &a_port);
    void disconnectServer();

private slots:
    void getMsgFromClient(const QString &);

signals:
    void newStringMsg(const QString &a_msg);
    void information(const QString &a_info);

private:
    std::shared_ptr<QtNetClient> m_client;
    std::thread                  m_async;
    QString m_pseudo;
};

#endif //__SIMPLECHATGUI_CHATCLIENT_H__
